using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

interface IBtnClick
{
    public void OnBtnClick(string btnName);
}



public interface IUIStack
{
    public void OnUIStackClose();
}




// UI的基础控制类
public class UICtrl : MonoBehaviour
{
    private Dictionary<string, List<UIBehaviour>> view = new Dictionary<string, List<UIBehaviour>>();

    private EventTrigger etall; // 事件
    private string hidepanel_name = "";

    public virtual void Awake()
    {
        this.FindChildrenControl<Button>();
        this.FindChildrenControl<Image>();
        this.FindChildrenControl<Text>();
        this.FindChildrenControl<Toggle>();
        this.FindChildrenControl<Slider>();
        this.FindChildrenControl<ScrollRect>();
        this.FindChildrenControl<InputField>();
        this.FindChildrenControl<Dropdown>();
    }



    /// <summary>
    /// 点击任意位置隐藏界面
    /// </summary>
    /// <param name="hidepanel_name">需要隐藏的界面名称</param>
    protected void AddEventTrigger_all(string hidepanel_name)
    {
        this.hidepanel_name = hidepanel_name;
        UnityAction<BaseEventData> click = new UnityAction<BaseEventData>(OnPointerClick_all);
        EventTrigger.Entry myclick = new EventTrigger.Entry();
        myclick.eventID = EventTriggerType.PointerClick;
        myclick.callback.AddListener(click);

        etall = gameObject.AddComponent<EventTrigger>();
        etall.triggers.Add(myclick);
    }
    protected void OnPointerClick_all(BaseEventData data)
    {
        UIMgr.Instance.HidePanel(this.hidepanel_name);
    }

     

   
    protected T GetControl<T>(string controlName)  where T : UIBehaviour // 得到对应名字的对应控件脚本
    {
        if (this.view.ContainsKey(controlName))
        {
            for (int i = 0; i < this.view[controlName].Count; ++i)
            {
                if (this.view[controlName][i] is T)
                    return this.view[controlName][i] as T;
            }
        }

        return null;
    }
     
    private void FindChildrenControl<T>()  where T : UIBehaviour  // 找到子对象对应的控件
    {
        T[] controls = this.GetComponentsInChildren<T>();
        for (int i = 0; i < controls.Length; ++i)
        {
            string objName = controls[i].gameObject.name;
            if (this.view.ContainsKey(objName))
                this.view[objName].Add(controls[i]);
            else
                this.view.Add(objName, new List<UIBehaviour>() { controls[i] });

            // 如果是按钮
            if (controls[i] is Button)
            {
                (controls[i] as Button).onClick.AddListener(() =>
                {
                    // 按钮按下的声音
                    // SoundMgr.Instance.PlayerBkMusic("bgm_slot");
                    SoundMgr.Instance.PlaySound("sfx_button");
                    // end

                    this.OnClick(objName);
                });
            }
            // end

            // 如果是单选框或者多选框
            else if (controls[i] is Toggle)
            {
                (controls[i] as Toggle).onValueChanged.AddListener((value) =>
                {
                    if (value)
                        SoundMgr.Instance.PlaySound("sfx_button");
                    this.OnValueChanged(objName, value);
                });
            }
            // end

            // 如果是下来列表
            else if (controls[i] is Dropdown)
            {
                (controls[i] as Dropdown).onValueChanged.AddListener((value) =>
                {
                    this.OnValueChanged(objName, value);
                });
            }
            // end

            // 如果是滑动条
            else if (controls[i] is Slider)
            {
                (controls[i] as Slider).onValueChanged.AddListener((value) =>
                {
                    this.OnValueChanged(objName, value);
                });
            }
            // end

            // 如果是文字输入
            else if (controls[i] is InputField)
            {
                (controls[i] as InputField).onValueChanged.AddListener((value) =>
                {
                    this.OnValueChanged(objName, value);
                });
            }
            // end

        }
    }

    #region event onClick.AddListener
    protected virtual void OnClick(string btnName)
    {

    }
    protected virtual void OnValueChanged(string btnName, bool value)
    {

    }
    protected virtual void OnValueChanged(string btnName, int value)
    {

    }
    protected virtual void OnValueChanged(string btnName, float value)
    {

    }
    protected virtual void OnValueChanged(string btnName, string value)
    {

    }

    #endregion

    
    public virtual void ShowMe() // 显示自己
    {
        gameObject.SetActive(true);
    }
     
    public virtual void HideMe()  // 隐藏自己（删除自己）
    {

    }
     
    public virtual void OnBtnClick(string btnName)
    {

    }

}
